package cn.iocoder.yudao.module.bpm.service.formdata;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.bpm.controller.admin.formdata.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.formdata.FormDataDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.bpm.convert.formdata.FormDataConvert;
import cn.iocoder.yudao.module.bpm.dal.mysql.formdata.FormDataMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;

/**
 * 表单详情 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class FormDataServiceImpl implements FormDataService {

    @Resource
    private FormDataMapper formDataMapper;

    @Override
    public Long createFormData(FormDataCreateReqVO createReqVO) {
        // 插入
        FormDataDO formData = FormDataConvert.INSTANCE.convert(createReqVO);
        formDataMapper.insert(formData);
        // 返回
        return formData.getId();
    }

    @Override
    public Long createMutiFormData(List<FormDataCreateReqVO> createReqVO) {
        for(FormDataCreateReqVO fd:createReqVO){
            // 插入
            FormDataDO formData = FormDataConvert.INSTANCE.convert(fd);
            formDataMapper.insert(formData);
        }
        return 1L;
    }

    @Override
    public void updateFormData(FormDataUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateFormDataExists(updateReqVO.getId());
        // 更新
        FormDataDO updateObj = FormDataConvert.INSTANCE.convert(updateReqVO);
        formDataMapper.updateById(updateObj);
    }

    @Override
    public void updateMutiFormData(List<FormDataUpdateReqVO> updateReqVO) {
        List list1 = updateReqVO;//修改后的表单数据
        List list2= formDataMapper.selectList(new FormDataExportReqVO().setFormDef(updateReqVO.get(0).getFormDef()));//获取原表单数据
        List<Long> listid1=updateReqVO.stream().map(FormDataUpdateReqVO::getId).collect(Collectors.toList());
        List<Long> listid2=formDataMapper.selectList(new FormDataExportReqVO().setFormDef(updateReqVO.get(0).getFormDef()))
                .stream().map(FormDataDO::getId).collect(Collectors.toList());
        for(int i = 0;i<list1.size();i++){
            FormDataUpdateReqVO flag = (FormDataUpdateReqVO) list1.get(i);
            if(flag.getId() == null){//表单项不存在id的话则说明是新增的
                //增加
                FormDataDO formData = FormDataConvert.INSTANCE.convert(flag);
                formDataMapper.insert(formData);
            }else{
                    if(!formDataMapper.selectById(flag.getId()).equals(flag)){//和原表单项不相等则修改
                        //修改
                        FormDataDO updateObj = FormDataConvert.INSTANCE.convert(flag);
                        formDataMapper.updateById(updateObj);
                    }
            }
        }
        for(int j = 0;j<list2.size();j++){
            if(!listid1.contains(listid2.get(j))){//修改后的表单不包含原表单项id说明该表单项已删除
                //删除
                formDataMapper.deleteById((FormDataDO) list2.get(j));
            }
        }
    }

    @Override
    public void deleteFormData(Long id) {
        // 校验存在
        this.validateFormDataExists(id);
        // 删除
        formDataMapper.deleteById(id);
    }

    @Override
    public void deleteBfdFormData(String formDef) {
        // 删除
        formDataMapper.deleteByFormdef(formDef);
    }

    private void validateFormDataExists(Long id) {
        if (formDataMapper.selectById(id) == null) {
            throw exception(FORM_DATA_NOT_EXISTS);
        }
    }

    @Override
    public FormDataDO getFormData(Long id) {
        return formDataMapper.selectById(id);
    }

    @Override
    public List<FormDataDO> getFormDataBfdList(FormDataExportReqVO exportReqVO) {
        return formDataMapper.selectList(exportReqVO);

    }

    @Override
    public List<FormDataDO> getFormDataList(Collection<Long> ids) {
        return formDataMapper.selectBatchIds(ids);
    }


    @Override
    public PageResult<FormDataDO> getFormDataPage(FormDataPageReqVO pageReqVO) {
        return formDataMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FormDataDO> getFormDataList(FormDataExportReqVO exportReqVO) {
        return formDataMapper.selectList(exportReqVO);
    }



}
